﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HH.Domain.Services
{
    public class ArticleService
    {
        public bool Create(Guid id,
            string category,
            string title,
            string content,
            string creator)
        {
            Entities.Article article = new Entities.Article();
            article.Id = id;
            article.Content = content;
            article.Title = title;
            article.CategoryText = category;
            article.Creator = creator;
            article.CreatedTime = DateTime.Now;
            article.PublishedTime = DateTime.Now;
            article.IsDeleted = false;
            article.IsEnabled = true;

            using(HHDbContext dbContext = new HHDbContext())
            {
                EntityRepository<Entities.Article> rep = new EntityRepository<Entities.Article>(dbContext);
                rep.Create(article);

                if(dbContext.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public PagedResult<Entities.Article> GetPagedList(int start, int limit)
        {
            using (HHDbContext dbContext = new HHDbContext())
            {
                EntityRepository<Entities.Article> rep = new EntityRepository<Entities.Article>(dbContext);
                List<Entities.Article> articles = rep.Query(p => p.IsDeleted == false && p.IsEnabled == true)
                    .Skip(start)
                    .Take(limit)
                    .OrderBy(p => p.CreatedTime).ToList();
                int total = rep.Count();
                return new PagedResult<Entities.Article>(articles, start, limit, total);
            }
        }
    }
}
